/*
* File usb_logger.h


*/


/******************************************************************************
 * Includes
 *****************************************************************************/
#include "usb_class.h"
#include "usb_descriptor.h"
#include "main.h"
#include "usbfs_dev_main.h"
#include "mqx_dev.h"

/******************************************************************************
 * Macro's
 *****************************************************************************/
#define LOGGER_MAX_QUEUE_ELEMS  (4) 

#define  LOGGER_BUFF_SIZE   (64)   /* report buffer size */


/* class specific requests */

/* for class specific requests */
//#define HIGH_BYTE_SHIFT                   (8) 
//#define MSB_MASK                          (0xFF00)
//#define USB_HID_REQUEST_DIR_MASK          (0x08)
//#define USB_HID_REQUEST_TYPE_MASK         (0x01)
//#define REPORT_SIZE                       (4)
//#define CLASS_REQ_DATA_SIZE               (0x01)
#define MAX_LOGGER_DEVICE                    (0x05)

 /******************************************************************************
 * Types
 *****************************************************************************/
typedef uint_32 LOGGER_HANDLE;

/* Callback function pointer structure for USB Logger EP service calls */
typedef void(_CODE_PTR_ USB_LOGGER_EP_SERVICE_CALL)(PTR_USB_EVENT_STRUCT event, pointer arg);

 
 /* structure to hold a request in the endpoint queue */
typedef struct _usb_class_logger_queue 
{
    _usb_device_handle handle;
    uint_8 channel;       
    uint_8_ptr app_buff; /* buffer to send */
    uint_32 size; /* size of the transfer */  
}USB_CLASS_LOGGER_QUEUE, *PTR_USB_CLASS_LOGGER_QUEUE;
 
/* USB class logger endpoint data */
  
typedef struct _usb_class_logger_endpoint 
{
    uint_8 endpoint; /* endpoint num */                    
    uint_8 type;     /* type of endpoint (interrupt, bulk or isochronous) */   
    uint_8 bin_consumer;/* the num of queued elements */
    uint_8 bin_producer;/* the num of de-queued elements */
    USB_CLASS_LOGGER_QUEUE queue[LOGGER_MAX_QUEUE_ELEMS]; /* queue data */  
}USB_CLASS_LOGGER_ENDPOINT;

/* contains the endpoint data for non control endpoints */
typedef struct _usb_class_logger_endpoint_data 
{    
    uint_8 count;  /* num of non control endpoints */  
    /* contains the endpoint info */
    /*Allocate Memory In App Layer*/    
    USB_CLASS_LOGGER_ENDPOINT *ep;       
}USB_CLASS_LOGGER_ENDPOINT_DATA, *PTR_USB_CLASS_LOGGER_ENDPOINT_DATA;


typedef struct _logger_variable_struct
{
	LOGGER_HANDLE app_handle;
	boolean logger_init;/* flag to check lower layer status*/
	uint_8 rpt_buf[LOGGER_BUFF_SIZE];/*report/data buff for mouse application*/
	USB_CLASS_LOGGER_ENDPOINT ep[LOGGER_DESC_ENDPOINT_COUNT];
	uint_8 app_request_params[2]; /* for get/set idle and protocol requests*/
}LOGGER_GLOBAL_VARIABLE_STRUCT, _PTR_ PTR_LOGGER_GLOBAL_VARIABLE_STRUCT; 



 /* Strucutre holding USB_LOGGER class state information*/
 typedef struct logger_device_struct
 {
   _usb_device_handle handle;   /* = USB_DEV_STATE_STRUCT_PTR: usbpru.h.
                                     see USB_Class_LOGGER_Init() */
   uint_32 user_handle;
   USB_CLASS_HANDLE class_handle;
   USB_ENDPOINTS *ep_desc_data;   /* Set to &usb_desc_ep in USB_class_logger_Init() 
                                     file usb_logger.c.
                                     usb_desc_ep is defined as type USB_ENDPOINTS
                                     in usb_descriptor.c.
                                     Provides info on the number of EPs 
                                     and their individual Typr, Dir and Packet Size.*/
   USB_CLASS_CALLBACK_STRUCT logger_class_callback;
   USB_REQ_CALLBACK_STRUCT       vendor_req_callback;
   USB_CLASS_SPECIFIC_HANDLER_CALLBACK_STRUCT param_callback;
   USB_CLASS_LOGGER_ENDPOINT_DATA logger_endpoint_data;
   /* for get/set idle and protocol requests*/
   //uint_8 class_request_params[2]; 
 }LOGGER_DEVICE_STRUCT, _PTR_ LOGGER_DEVICE_STRUCT_PTR;
 

#ifndef __main_h_
#define __main_h_


#define MAIN_TASK 1


extern void Main_task(uint_32);


#endif /* __main_h_ */



/******************************************************************************
 * Includes
 *****************************************************************************/
#include "usb_descriptor.h"


/*****************************************************************************
 * Global Functions
 *****************************************************************************/

void Main_Task( uint_32 param );
void TestApp_Init(void);
void Endpoint_Init(void);

/*****************************************************************************
 *
 * Structures for operation of the M52259DEMO Board
 *
 *****************************************************************************/
 
 /****************************************************************************
  *	Control structure and buffer for receiving endpoint OUT (receive) packets 
  *	from the host PC.
  * Operation:
  * 1) pkt_rec is set by the EPn TOK_DNE interrupt service routine 
  *  	USB_EPn_SERVICE_CALL() (usb_logger.c) when a packet has been received 
  *  	and is available to be transferred to the 
  *  	EPn receive packet buffer ep_pkt_buf.
  *  	It is cleared in EPn_PACKET_REC() (main.c) after the packet is 
  *  	transferred.
  * 2) overrun_err is set if a new packet is received before the previous 
  *  	packet is transferred to the EPn receive packet buffer ep_pkt_buf.
  * 3) buffer_ptr is a pointer to the reveived packet. It is set by the 
  *  	EPn TOK_DNE interrupt service routine 
  *  	USB_EPn_SERVICE_CALL() (usb_logger.c) when a packet has been received
  *  	and is available to be transferred to the EPn receive packet 
  *    	buffer ep_pkt_buf. 
  ***************************************************************************/   	
 typedef struct ep_rx_ctrl
 {
 	boolean   	pkt_rec;		// True when a packet has arrived and is 
 	                        	// available to be read from the receive buffer.
 	boolean		overrun_err;	// EP1 receive buffer overrun error.
 	uint_8_ptr	buffer_ptr;		// Pointer to the receive buffer.
 	uint_32		len;			// Number of bytes received in the packet.
 	uint_8   	ep_pkt_buf[USB_EP_PKT_SIZE];	// EP1 receive packet buffer. 	
 }EP_RX_CTRL, _PTR_ EP_RX_CTRL_PTR;

 
 /*****************************************************************************
  *
  * GLOBAL VARIABLES
  *
  *****************************************************************************/
  
  /* Control structures for EP1 & EP3 OUT (receive) packet transactions */
//  EP_RX_CTRL ep1_rx_ctrl;
//  EP_RX_CTRL ep3_rx_ctrl;
  
  
  
/*****************************************************************************
 * Global Functions
 *****************************************************************************/

/**************************************************************************//*!
 *
 * @name  USB_Class_LOGGER_Init
 *
 * @brief The funtion initializes the Device and Controller layer 
 *
 * @param void
 *
 * @return status       
 *         USB_OK           : When Successfull 
 *         Others           : Errors
 ******************************************************************************
 *
 *This function initializes the HID Class layer and layers it is dependent on 
 *
 *****************************************************************************/

extern LOGGER_HANDLE USB_Class_LOGGER_Init(void);

/**************************************************************************//*!
 *
 * @name  USB_Class_Logger_Event
 *
 * @brief The funtion initializes LOGGER endpoints 
 *
 * @param handle          handle to Identify the controller
 * @param event           pointer to event structure
 * @param val             gives the configuration value 
 *
 * @return None       
 *
 *****************************************************************************/
void USB_Class_Logger_Event(uint_8 event, void* val,void * arg); 

/******************************************************************************
 * 
 *    @name        USB_App_Callback
 *    
 *    @brief       This function handles the callback  
 *                  
 *    @param       handle : handle to Identify the controller
 *    @param       event_type : value of the event
 *    @param       val : gives the configuration value 
 * 
 *    @return      None
 *
 *****************************************************************************/
void USB_App_Callback(uint_8 event_type, void* val,pointer arg);

/**************************************************************************//*!
 *
 * @name  USB_Class_EP2_Send_Data
 *
 * @brief 
 *
 * @param ep_num          :   endpoint num 
 * @param app_buff        :   buffer to send
 * @param size            :   length of the transfer   
 *
 * @return status       
 *         USB_OK           	: When Successfull 
 *         USBERR_DEVICE_BUSY   : Errors
 *****************************************************************************/
uint_8 USB_Class_EP2_Send_Data(
                                uint_8 ep_num,/*[IN]*/
                                uint_8_ptr app_buff,/*[IN]*/
                                uint_32 size /*[IN]*/
                               ); 

/**************************************************************************//*!
 *
 * @name  USB_Class_EP4_Send_Data
 *
 * @brief : Initiates transmission of packets via EP4.
 *
 * @param ep_num          :   endpoint num 
 * @param app_buff        :   buffer to send
 * @param size            :   length of the transfer   
 *
 * @return status       
 *         USB_OK           	: When Successfull 
 *         USBERR_DEVICE_BUSY   : Errors - queue full.
 *****************************************************************************/
uint_8 USB_Class_EP4_Send_Data(
                                uint_8 ep_num,/*[IN]*/
                                uint_8_ptr app_buff,/*[IN]*/
                                uint_32 size /*[IN]*/
                               ); 

/******************************************************************************
 * 
 *    @name        USB_App_Param_Callback
 *    
 *    @brief       This function handles the callback for Get/Set report req  
 *                  
 *    @param       request  :  request type
 *    @param       value    :  give report type and id
 *    @param       data     :  pointer to the data 
 *    @param       size     :  size of the transfer
 *
 *    @return      status
 *                  USB_OK  :  if successful
 *                  else return error
 *
 *****************************************************************************/
 uint_8 USB_App_Param_Callback
 (
 	uint_8 request, 
 	uint_16 value, 
 	uint_8_ptr* data, 
    uint_32* size,
    pointer arg
) ;

/***************************************************************************
 *
 * @name  USB_EP1_SERVICE_CALL
 *
 * @brief Callback function to be executed following a TOK_DNE event on EP1. 
 *
 * @param event           Pointer to a USB_EVENT_STRUCT for EP1
 * @param arg             Pointer to the LOGGER_DEVICE_STRUCT
 *
 * @return None       
 *
 *****************************************************************************/
void USB_EP1_SERVICE_CALL(PTR_USB_EVENT_STRUCT, pointer);


/***************************************************************************
 *
 * @name  USB_EP2_SERVICE_CALL
 *
 * @brief Callback function to be executed following a TOK_DNE event on EP2. 
 *
 * @param event           Pointer to a USB_EVENT_STRUCT for EP2
 * @param arg             Pointer to the LOGGER_DEVICE_STRUCT
 *
 * @return None       
 *
 *****************************************************************************/
void USB_EP2_SERVICE_CALL(PTR_USB_EVENT_STRUCT, pointer);


/***************************************************************************
 *
 * @name  USB_EP3_SERVICE_CALL
 *
 * @brief Callback function to be executed following a TOK_DNE event on EP3. 
 *
 * @param event           Pointer to a USB_EVENT_STRUCT for EP3
 * @param arg             Pointer to the LOGGER_DEVICE_STRUCT
 *
 * @return None       
 *
 *****************************************************************************/
void USB_EP3_SERVICE_CALL(PTR_USB_EVENT_STRUCT, pointer);


/***************************************************************************
 *
 * @name  USB_EP1_SERVICE_CALL
 *
 * @brief Callback function to be executed following a TOK_DNE event on EP4. 
 *
 * @param event           Pointer to a USB_EVENT_STRUCT for EP4
 * @param arg             Pointer to the LOGGER_DEVICE_STRUCT
 *
 * @return None       
 *
 *****************************************************************************/
void USB_EP4_SERVICE_CALL(PTR_USB_EVENT_STRUCT, pointer);


/**************************************************************************//*!
 *
 * @name  USB_Logger_Other_Requests
 *
 * @brief The funtion provides flexibilty to add class and vendor specific
 *        requests 
 *
 * @param handle:
 * @param setup_packet:     setup packet recieved      
 * @param data:             data to be send back
 * @param size:             size to be returned    
 *
 * @return status:       
 *                        USB_OK : When Successfull       
 *                        Others : When Error
 *
 *****************************************************************************/
uint_8 USB_Logger_Other_Requests(USB_SETUP_STRUCT * setup_packet, 
                          uint_8_ptr *data, 
                          uint_32 *size,
                          pointer arg);
                          
                          
 /*************************************************************************//*!
 *
 * @name  USB_Logger_Get_Device_Ptr
 *
 * @brief The funtion gets the device pointer from device array .
 *
 * @param handle  index in device array.
 * @return returns returns pointer to LOGGER device structure..      
 *
 *****************************************************************************/
static LOGGER_DEVICE_STRUCT_PTR USB_Logger_Get_Device_Ptr(LOGGER_HANDLE handle);

 /*************************************************************************//*!
 *
 * @name  LOGGER_USB_Map_Ep_To_Struct_Index
 *
 * @brief The funtion maps the endpoint num to the index of the ep data 
 *           structure
 *
 * @param handle          handle to identify the controller
 * @param ep_num          endpoint num
 *
 * @return index          mapped index       
 *
 *****************************************************************************/
static uint_8 LOGGER_USB_Map_Ep_To_Struct_Index(LOGGER_DEVICE_STRUCT_PTR devicePtr,
uint_8 ep_num); 

